<?php
/**
 * WpHttpRequest.php
 * 
 * This file contains the class WpHttpRequest to be used for http requests
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage web.request
 */

include_once(dirname(__FILE__).'/IWpRequest.php');

/**
 * The class to be used for http requests
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage web.request
 */
class WpHttpRequest implements IWpRequest
{
	protected $params = array();
	
	/**
	 * The class constructor
	 * @param object $config[optional] The configuration object.
	 */
	public function __construct($config=null)
	{
		$this->params = $_REQUEST;
	}
	
	/**
   * Checks wether the request contains a file
   * @param string $name The name of the field, used for the file
   * @return bool True if the file was sent using the specified field, false else
   */
	public function containsFile($name)
	{
		return (array_key_exists($name, $_FILES) &&
				$_FILES[$name]['size'] > 0 && $_FILES[$name]['tmp_name'] !== '');
	}
	
	/**
   * Retrieves the filename of the uploaded file specified by upload field name
   * @param string $name The name of the field used for file upload
   * @return The filename of the file specified by field name
   */
	public function getFilename($name)
	{
		if (!$this->containsFile($name)) {
			return '';
		}
		
		return $_FILES[$name]['name'];
	}
	
	/**
   * Retrieves the value of the parameter specified by name
   * @param string $name The name of the parameter to return the value of
   * @return mixed The value of the parameter
   */
	public function getParam($name)
	{
		if(isset($this->params[$name])) {
			return $this->params[$name];
		}
		
		return null;
	}
	
	/**
   * Retrieves a list of parameter names
   * @return An array of parameters names
   */
	public function getParamNames()
	{
		return array_keys($this->params);
	}
	
	/**
   * Retrieves the request header value (if available)
   * @param string $name The name of the header value to retrieve
   * @return The header value
   */
	public function getHeader($name)
	{
		$strName = 'HTTP_'.strtoupper(str_replace('-','_', $name));
		if(isset($_SERVER[$strName]))
		{
			return $_SERVER[$strName];
		}
		return null;
	}
	
	/**
   * Determines wether a parameter is set or not
   * @param string $name The name of the parameter tocontroller check
   * @return True if the parameter is set, false else
   */
	public function issetParam($name)
	{
		return isset($this->params[$name]);
	}
	
	/**
   * Performs a redirect to another location
   * @param string $location The location to redirect to
   */
	public function redirect($location)
	{
		header('Location:'.$location);
		die;
	}
	
	/**
   * Saves the file, specified by upload field name to the server
   * @param string $name The name of the field used for file upload
   * @param string $path The path to save the file to
   * @param string $newfilename[optional] The name to use for the file instead of the original one
   * @return bool True if successfully done, false else
   */
	public function saveFile($name, $path, $newfilename = '')
	{
		if (!$this->containsFile($name)) {
			return false;
		}
		
		if (substr($path, -1, 1) !== '/') {
			$path .= '/';
		}
		$file = $newfilename === '' ? $_FILES[$name]['name'] : $newfilename;
		
		$p = $path.$file;
		if (!move_uploaded_file($_FILES[$name]['tmp_name'], $p)) {
			return false;
		}
		
		return true;	
	}
}

/* End of file WpHttpRequest.php */
/* Location: ./wildpack/web/request/WpHttpRequest.php */ 
